#include "search.h"
#include "ui_search.h"
#include "readersql.h"
#include <QsqlQuery>
#include <QSqlDatabase>
#include <QMessageBox>
Search::Search(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Search)
{
    ui->setupUi(this);
    global::read->m_db;
    
}

Search::~Search()
{
    delete ui;
}

void Search::on_OK_clicked()
{ QString s = QString(ui->ID->text());
  QSqlQuery sql(global::read->m_db);
  QString strSql = QString("SELECT * FROM Reader WHERE ID=%1").arg(s);
  sql.exec(strSql);
  if(sql.next())
  {qDebug()<<"Found";
   ui->ID_2->clear();
   ui->password->clear();
   ui->age->clear();
   ui->grade->clear();
   ui->major->clear();
   ui->studentid->clear();
   ui->phone->clear();
   ui->email->clear();
   ui->access->clear();
   ui->name->clear();
   ui->ID_2->insert(sql.value(0).toString());
   ui->password->insert(sql.value(1).toString());
   ui->name->insert(sql.value(2).toString());
   ui->age->insert(QString::number(sql.value(3).toUInt()));
   ui->grade->insert(QString::number(sql.value(4).toUInt()));
   ui->major->insert(sql.value(5).toString());
   ui->studentid->insert(sql.value(6).toString());
   ui->phone->insert(sql.value(7).toString());
   ui->email->insert(sql.value(8).toString());
   ui->access->insert(QString::number(sql.value(9).toUInt()));


  }



}


void Search::on_pushButton_clicked()
{QSqlQuery sql(global::read->m_db);
    QString s =QString("update Reader set password='%1',name='%2',age=%3,grade=%4,major='%5',studentid=%6,phone='%7',email='%8',authority=%9 where ID='%10'")
            .arg(ui->password->text())
            .arg(ui->name->text())
            .arg(ui->age->text().toUInt())
            .arg(ui->grade->text().toUInt())
            .arg(ui->major->text())
            .arg(ui->studentid->text())
            .arg(ui->phone->text())
            .arg(ui->email->text())
            .arg(ui->access->text().toUInt())
            .arg(ui->ID_2->text());
 qDebug()<<s;
 sql.exec(s);
 QMessageBox::information(this,tr("提示对话框"),QString("提交成功！"),QMessageBox::Ok);


}

